//
//  FindPwdBackViewController.m
//  LoverGo
//
//  Created by liudonghuan on 14-9-13.
//  Copyright (c) 2014年 yexiao. All rights reserved.
//

#import "FindPwdBackViewController.h"
#import "LGUtil.h"
#import "Toast+UIView.h"
@interface FindPwdBackViewController ()

@end

@implementation FindPwdBackViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
-(void)loadView
{
    [self dinglan];
    _findView = [[FindPwdBackView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _findView.findDelegate = self;
    self.hidesBottomBarWhenPushed = YES;
    [self setView:_findView];
}
-(void)dinglan{
    UIButton * backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,20, 20)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(dinglanBackBtn) forControlEvents:UIControlEventTouchUpInside] ;
    [backBtn setShowsTouchWhenHighlighted:YES] ;
    UIBarButtonItem *backbutton = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backbutton;
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(90, 0, 140, 30)];
    title.textAlignment = UITextAlignmentCenter;
    title.textColor = LGCOLOR_blue;
    title.text = @"找回密码";
    self.navigationItem.titleView = title;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"barBG.png"] forBarMetrics:UIBarMetricsDefault];
}
-(void)dinglanBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)toGetCheckNum:(FindPwdBackView *)view
{
    _findView.getCheckNumBtn.enabled = NO;
    if ([self checkTel:_findView.phoneNumTF.text]) {
        NSDictionary *para = [NSDictionary dictionaryWithObjectsAndKeys:
                              _findView.phoneNumTF.text,@"mobile",
                              nil];
        SVHTTPClient *svc = [[SVHTTPClient alloc] init];
        [svc POST:@"http://server.lover-go.com/appUser/getCheckNum" parameters:para completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
            
            if (error) {
                [[[[UIApplication sharedApplication] delegate] window] makeToast:@"网络连接失败"
                                                                        duration:1.0
                                                                        position:@"center"];
                _findView.getCheckNumBtn.enabled = YES;

            }else{
                NSError *jsonError = nil;
                id jsonObject = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&jsonError];
                if (jsonError) {
                    [[[[UIApplication sharedApplication] delegate] window] makeToast:@"数据解析异常"
                                                                            duration:1.0
                                                                            position:@"center"];
                    _findView.getCheckNumBtn.enabled = YES;
                    return;
                }else{
                    NSArray *arr = [jsonObject objectForKey:@"data"];
                    int status = [[jsonObject objectForKey:@"status"]intValue];
                    if (status == 0) {//接收成功
                        _checknum = [[arr objectAtIndex:0]objectForKey:@"checkNum"];
                        _findView.step1V.hidden = YES;
                        _findView.step2V.hidden = NO;
                        _findView.phoneNumLab.text = [_findView.phoneNumTF text];
                    }else if (status == 1){//该用户未注册
                        [[[[UIApplication sharedApplication] delegate] window] makeToast:@"该手机号尚未注册"
                                                                                duration:1.0
                                                                                position:@"center"];
                        _findView.getCheckNumBtn.enabled = YES;
                        return;
                        
                    }else{//其他错误
                        [[[[UIApplication sharedApplication] delegate] window] makeToast:@"发生了未知错误，通过意见反馈联系我们"
                                                                                duration:1.0
                                                                                position:@"center"];
                        _findView.getCheckNumBtn.enabled = YES;
                        return;
                    }
                }

               

            }
        }];
        
    }else{
        _findView.getCheckNumBtn.enabled = YES;
    }
}

-(void)checkNum:(FindPwdBackView *)view
{
    _findView.checkBtn.enabled = NO;
    if (![[_findView.checkNumTF text] compare:@""]) {
        [[[[UIApplication sharedApplication] delegate] window] makeToast:@"验证码不能为空"
                                                                duration:1.0
                                                                position:@"center"];
         _findView.checkBtn.enabled = YES;
    }else if (![[_findView.checkNumTF text] isEqualToString:_checknum]){
        [[[[UIApplication sharedApplication] delegate] window] makeToast:@"请输入正确验证码"
                                                                duration:1.0
                                                                position:@"center"];
        _findView.checkBtn.enabled = YES;
    }else{
        view.step2V.hidden = YES;
        view.step3V.hidden = NO;
    }
    
}
-(void)submit:(FindPwdBackView *)view
{
    _findView.submitBtn.enabled = NO;
    if (![[view.psdTF text] compare:@""] || ![[view.psdCheckTF text] compare:@""] ) {
        [[[[UIApplication sharedApplication] delegate] window] makeToast:@"密码栏不能为空"
                                                                duration:1.0
                                                                position:@"center"];
         _findView.submitBtn.enabled = YES;
    }else if(![[view.psdTF text] isEqualToString:[view.psdCheckTF text]]){
        [[[[UIApplication sharedApplication] delegate] window] makeToast:@"两次密码不相同"
                                                                duration:1.0
                                                                position:@"center"];
        _findView.submitBtn.enabled = YES;
    }else{
        NSDictionary *para = [NSDictionary dictionaryWithObjectsAndKeys:
                              _findView.phoneNumTF.text,@"mobile",
                              _findView.psdTF.text,@"passWord",
                              nil];
        SVHTTPClient *svc = [[SVHTTPClient alloc] init];
        [svc POST:@"http://server.lover-go.com/appUser/getBackPassword" parameters:para completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
            if (error) {
                [[[[UIApplication sharedApplication] delegate] window] makeToast:@"网络连接错误"
                                                                        duration:1.0
                                                                        position:@"center"];
                _findView.submitBtn.enabled = YES;

            }else{
                NSError *jsonError = nil;
                id jsonObject = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&jsonError];
                if (jsonError) {
                    [[[[UIApplication sharedApplication] delegate] window] makeToast:@"数据解析异常"
                                                                            duration:1.0
                                                                            position:@"center"];
                    _findView.submitBtn.enabled = YES;
                    return;
                }else{
                    int status = [[jsonObject objectForKey:@"status"]intValue];
                    if (status == 0) {//接收成功
                        [[[[UIApplication sharedApplication] delegate] window] makeToast:@"找回密码成功"
                                                                                duration:1.0
                                                                                position:@"center"];
                        [self.navigationController popViewControllerAnimated:YES];
                        
                    }else{//其他错误
                        [[[[UIApplication sharedApplication] delegate] window] makeToast:@"发生了未知错误，通过意见反馈联系我们"
                                                                                duration:1.0
                                                                                position:@"center"];
                        _findView.submitBtn.enabled = YES;
                        return;
                    }
                    

                }
            }
            
        }];

    }
}
- (BOOL)checkTel:(NSString *)str

{
    
    if ([str length] == 0) {
        
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"", nil) message:NSLocalizedString(@"手机号不能为空", nil) delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        return NO;
        
    }
    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:str];
    if (!isMatch) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        return NO;
    }
  return YES;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"找回密码"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"找回密码"];
}

@end
